package com.tsingtree.modular.controller.view;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tsingtree.base.auth.annotion.Permission;
import com.tsingtree.base.pojo.page.LayuiPageInfo;
import com.tsingtree.modular.entity.NanSeller;
import com.tsingtree.modular.entity.NanSellerDevice;
import com.tsingtree.modular.model.params.NanSellerDeviceParam;
import com.tsingtree.modular.service.NanSellerDeviceService;
import com.tsingtree.modular.service.NanSellerService;
import com.tsingtree.roses.core.base.controller.BaseController;
import com.tsingtree.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 运营组-设备控制器
 *
 * @author jagger
 */
@Controller
@RequestMapping("/nanSellerDevice")
public class NanSellerDeviceController extends BaseController {

    private String PREFIX = "/nanSellerDevice";

    @Autowired
    private NanSellerDeviceService nanSellerDeviceService;

    @Autowired
    private NanSellerService nanSellerService;

    /**
     * 跳转到主页面
     *
     * @author jagger
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/nanSellerDevice.html";
    }

    /**
     * 新增页面
     *
     * @author jagger
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/nanSellerDevice_add.html";
    }

    /**
     * 编辑页面
     *
     * @author jagger
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/nanSellerDevice_edit.html";
    }

    /**
     * 新增接口
     *
     * @author jagger
     */
    @RequestMapping("/addItem")
    @Permission
    @ResponseBody
    public ResponseData addItem(NanSellerDeviceParam nanSellerDeviceParam) {
        int count = nanSellerDeviceService.count(new QueryWrapper<NanSellerDevice>().eq("code", nanSellerDeviceParam.getCode()));
        if(count > 0){
            return ResponseData.error("该编码已存在");
        }
        this.nanSellerDeviceService.add(nanSellerDeviceParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author jagger
     */
    @RequestMapping("/editItem")
    @Permission
    @ResponseBody
    public ResponseData editItem(NanSellerDeviceParam nanSellerDeviceParam) {
        NanSellerDevice nanSellerDevice = nanSellerDeviceService.getById(nanSellerDeviceParam.getId());
        if(!nanSellerDevice.getCode().equals(nanSellerDeviceParam.getCode())){
            return ResponseData.error("编码不能改变");
        }
        this.nanSellerDeviceService.update(nanSellerDeviceParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author jagger
     */
    @RequestMapping("/delete")
    @Permission
    @ResponseBody
    public ResponseData delete(NanSellerDeviceParam nanSellerDeviceParam) {
        NanSellerDevice nanSellerDevice = nanSellerDeviceService.getById(nanSellerDeviceParam.getId());
        nanSellerDevice.setIsForbid(!nanSellerDevice.getIsForbid());
        this.nanSellerDeviceService.updateById(nanSellerDevice);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author jagger
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(NanSellerDeviceParam nanSellerDeviceParam) {
        NanSellerDevice detail = this.nanSellerDeviceService.getById(nanSellerDeviceParam.getId());
        NanSeller nanSeller = nanSellerService.getById(detail.getSellerId());
        detail.setSellerName(nanSeller.getName());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jagger
     */
    @ResponseBody
    @Permission
    @RequestMapping("/list")
    public LayuiPageInfo list(NanSellerDeviceParam nanSellerDeviceParam) {
        return this.nanSellerDeviceService.findPageBySpec(nanSellerDeviceParam);
    }

    /**
     * 获取所有设备列表（用于下拉选择）
     *
     * @author jagger
     */
    @ResponseBody
    @RequestMapping("/listAll")
    public ResponseData listAll() {
        return ResponseData.success(this.nanSellerDeviceService.list(new QueryWrapper<NanSellerDevice>().eq("is_forbid", false)));
    }

}


